package xkp.lesson5;

import sun.rmi.runtime.Log;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class LoginUi extends JFrame {
    public LoginUi(){
        //设置窗体尺寸
        this.setSize(350,250);
        //禁止改变大小
        this.setResizable(false);
        this.setTitle("登录");
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //LoginUI没有任何元素，希望在其中添加元素
        //JButton按钮
        //入如果需要在JFrame中添加元素，必须要设置布局管理器
        //指定JFrame该如何布局
        //此时指定布局为流式布局
        //流式布局管理器默认水平居中
        //FlowLayout flowLayout = new FlowLayout(FlowLayout.RIGHT);
        FlowLayout flowLayout = new FlowLayout(FlowLayout.CENTER);
        this.setLayout(flowLayout);


        //添加元素
        JLabel jLabel = new JLabel("欢迎使用爬虫项目");
        jLabel.setPreferredSize(new Dimension(300,40));
        //设置JLable文本水平居中
        jLabel.setHorizontalAlignment(SwingConstants.CENTER);
        this.add(jLabel);

        JPanel jPanelUser = new JPanel();

        JLabel userLable = new JLabel("账号:");
        this.add(userLable);
        jPanelUser.add(userLable);

        JTextField userNameText = new JTextField();
        userNameText.setPreferredSize(new Dimension(250,30));
        this.add(userNameText);
        jPanelUser.add(userNameText);
        this.add(jPanelUser);


        JPanel pwdPanel = new JPanel();
        JLabel pwdLable = new JLabel("密码:");

        pwdPanel.add(pwdLable);


        JPasswordField userPwdText = new JPasswordField();
        userPwdText.setPreferredSize(new Dimension(250,30));
        pwdPanel.add(userPwdText);

        this.add(pwdPanel);


        JButton logBtn = new JButton("登录");
        this.add(logBtn);

        logBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String userName = userNameText.getText().trim();
                String userPwd = String.valueOf(userPwdText.getPassword()).trim();
                System.out.println(userName+":"+userPwd);
                if(userName.equals("张三")&&userPwd.equals("123")){
                    //JOptionPane.showMessageDialog(null, "用户名或密码正确");
                    //需要让当前登录页面关闭
                    LoginUi.this.setVisible(false);
                    //跳转到主页面
                    new MainUi();

                }else{
                    JOptionPane.showMessageDialog(null, "用户名或密码错误");
                }
            }
        });
        this.setVisible(true);
    }
}
